<?php
$objXajax->registerFunction("fmfrmreportcurrent_load");
$objXajax->registerFunction("fmfrmreportcurrent_loadRevenue");
$objXajax->registerFunction("fmfrmreportcurrent_setPhonenumber");
$objXajax->registerFunction("fmfrmreportcurrent_saveSMS");

function fmfrmreportcurrent_load(){     

    global $lang;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrData = fmfrmreportcurrent_createArray();
    $count = count($arrData);
     
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/fm_reportcurrent_grid.html');
    $oTbs->MergeBlock('blk_data', $arrData); 
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('fmfrmreportcurrent_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('fmfrmreportcurrent_tbl',true);");  
    $objResponse->addScript('changeWaiting(1);');                     
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function fmfrmreportcurrent_createArray(){
    global $objDbUpdate,$tbl_postfix;
    $sql = "  select h.id, r.name as roomname,h.timestart as timebegin,h.timeend as timeend,
   TIMESTAMPDIFF(MINUTE,h.timestart,h.timeend) as timerent,money_rent,total,price_voucher,arising_price,arising_description,
   u1.username as userstart ,u2.username as userend  from fm_current_book$tbl_postfix as h join fm_room r ON h.room_id = r.id 
    join tbl_user as u1 ON h.user_created = u1.id join tbl_user as u2 ON h.user_update = u2.id
     where  h.flgcal=1 and datediff(now(),h.timeend)=0 and h.flg_cancel = 0  order by timeend"; 
    $arr = array();    
    try{
        $arr = $objDbUpdate->GetArray($sql);
        $count = count($arr);
        for($i=0;$i<$count;$i++){
            $arr[$i]['money_product']=(int)$arr[$i]['total'] - 
            (int)$arr[$i]['money_rent']+(int)$arr[$i]['price_voucher']+(int)$arr[$i]['arising_price'];
            $arr[$i]['timerent']= ConvertTimeToHM($arr[$i]['timerent']) ;
            
        }

    }
    catch(Exception $e){
        $arr = array();
    } 
    return  $arr  ;
} 

function  fmfrmreportcurrent_summary(){
    global $objDbUpdate,$hotel_id;
    $sql= "select sum(TIMESTAMPDIFF(MINUTE,timestart,timeend)) as timerent ,
    sum(num_voucher) as numvoucher,
    count(id) as numrent,sum(money_rent) as money_rent,sum(price_voucher) as money_voucher
    ,sum(total) as money_total,sum(arising_price) as money_arising
     from fm_current_book_$hotel_id WHERE datediff(timeend,now())=0  and flg_cancel = 0
    "; 
    $arr =  $objDbUpdate->GetArray($sql);
    return $arr;
}

function fmfrmreportcurrent_loadRevenue(){
    global $objDbUpdate,$hotel_id,$lang;
    $objResponse = new xajaxResponse(); 
    $arr = fmfrmreportcurrent_summary();
    $arr[0]['timerent'] = ConvertTimeToHM($arr[0]['timerent']);

    $money_voucher = $arr[0]['money_voucher'];
    $money_total = $arr[0]['money_total'];
    $money_arising = $arr[0]['money_arising'];
    $money_rent = $arr[0]['money_rent'];
    $arr[0]['money_product'] = $money_total - $money_rent+$money_voucher+$money_arising;
    $to_time = date('H:i:s');
    $to_date = date('d/m/Y');
    $money_get = 0;
    $money_pay =0;
    $sqlpay = "select sum(money) as money,flg from cus_pay_$hotel_id where datediff(date_created,now())=0 group by flg ";
    $arrPay = $objDbUpdate->GetArray($sqlpay);
    for($i=0;$i<count($arrPay);$i++){
        if($arrPay[$i]['flg']==1){
           $money_get = $arrPay[$i]['money']; 
        }else{
            $money_pay = $arrPay[$i]['money']; 
        }
    }
    $money_get=$money_get==''?0:$money_get;
    $money_pay=$money_pay==''?0:$money_pay;
    $money_revenue = $money_total - $money_pay + $money_get;
    $arr[0]['money_revenue']=$money_revenue;
    $arr[0]['money_pay']=$money_pay;
    $arr[0]['money_get']=$money_get;
    $arr[0]['to_time']=$to_time;
    $arr[0]['to_date']=$to_date;
    
    /* Bắt đầu chuẩn bị phần này chuẩn bị cho phần tin nhắn*/
    $numrent      = FormatMoney($arr[0]['numrent']) ; 
    $timerent     = $arr[0]['timerent'] ;
    $money_rent   = FormatMoney($money_rent) ; 
    $money_product= FormatMoney($arr[0]['money_product']) ; 
    $money_arising= FormatMoney($money_arising) ; 
    $numvoucher   = FormatMoney($arr[0]['numvoucher']) ; 
    $money_voucher= FormatMoney($money_voucher) ; 
    $money_pay    = FormatMoney($money_pay) ; 
    $money_get    = FormatMoney($money_get) ; 
    $money_revenue= FormatMoney($money_revenue) ;
    
    $smscontent = SMS_COUNT_RENT.$numrent. SMS_SYSTEM_DOWNLINE.SMS_TIME_RENT.$timerent. 
    SMS_SYSTEM_DOWNLINE.SMS_MONEY_RENT.$money_rent.SMS_SYSTEM_DOWNLINE.SMS_MONEY_PRODUCT.
    $money_product. SMS_SYSTEM_DOWNLINE.SMS_MONEY_ARISING.$money_arising.SMS_SYSTEM_DOWNLINE.
    SMS_NUM_VOUCHER.$numvoucher.SMS_SYSTEM_DOWNLINE.SMS_MONEY_VOUCHER.$money_voucher.SMS_SYSTEM_DOWNLINE.
    SMS_MONEY_GET.$money_get.SMS_SYSTEM_DOWNLINE.SMS_MONEY_PAY.$money_pay.SMS_SYSTEM_DOWNLINE.SMS_MONEY_REVENUE.
    $money_revenue.SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_CAL_FROM.$to_time. SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_DATE.$to_date; 
    $objResponse->addScript("fmfrmreportcurrent_smscontent='$smscontent';");
    
    /* Kết thúc chuẩn bị phần này chuẩn bị cho phần tin nhắn*/    

    
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/fm_reportcurrent_now.html');
    $oTbs->MergeBlock('blk_now', $arr);
    $objResponse->addAssign('fmfrmreportcurrent_divleft', 'innerHTML', $oTbs->Source);
    
    $oTbs_sms = new clsTinyButStrong();
    $oTbs_sms->LoadTemplate('templates/'.$lang.'/customer/fm_reportcurrent_sms.html');  
    $oTbs_sms->MergeBlock('blk_sms', $arr);
    $objResponse->addAssign('fmfrmreportcurrent_divsmscontent', 'innerHTML', $oTbs_sms->Source); 
    return $objResponse->getXML();
   
}

function fmfrmreportcurrent_setPhonenumber(){
      global $lang,$objDbUpdate,$hotel_id;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $sql = 'select phone_auto_send_sms as phone from cus_config where service_id = '.$hotel_id;
    $arrPhone = $objDbUpdate->GetArray($sql);
    $phone = $arrPhone[0]['phone'];     
    $objResponse->addScript("$('#fmfrmreportcurrent_txtphonenumber').val('$phone'); "); 
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax  
}
function fmfrmreportcurrent_saveSMS($phonenumber,$fmfrmreportcurrent_smscontent){
    //echo strlen($fmfrmreportcurrent_smscontent); die();
     global $objDbSelect,$user_id,$hotel_id;
     $objResponse = new xajaxResponse(); 
     $arrPhone = explode(';',$phonenumber);
     $num_phone = count($arrPhone);
     $sql = "INSERT INTO tbl_sms(date_created, sms_type, sms_content, service_id, phone_number, num_phone, user_created, sms_timesend)
     values(now(),1,'$fmfrmreportcurrent_smscontent',$hotel_id,'$phonenumber',$num_phone,$user_id,now())" ;
     if($objDbSelect->Execute($sql)){
       $objResponse->addScript("$('#fmfrmreportcurrent_divsms').dialog('close');");     
       $objResponse->addAlert("Bạn đã gửi tin nhắn thành công");     
     }     
    
     return $objResponse->getXML(); 
}

# end region process data
?>
